package com.banciyuan.bcywebview.biz.setting;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.dialog.WaitLightDialog;

import de.greenrobot.daoexample.model.AppSetting;
import de.greenrobot.daoexample.model.TimeLineSetting;


public class MainSettingActivity extends BaseActivity implements View.OnClickListener {
    private View mulitSettingContainer;
    private View viewTraitorDesc;
    private ImageView recommend;
    private ImageView cos;
    private ImageView draw;
    private ImageView novel;
    private ImageView daily;
    private ImageView group;
    private ImageView goods;
    private ImageView traitor;
    private AppSetting appSetting;
    private TimeLineSetting initialSetting;
    private WaitLightDialog waitLightDialog;
    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage_setting_layout);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    private void setStatus() {
        if (appSetting != null && appSetting.getTimeline() != null) {
            setMultiTimelineStatus();
            if (appSetting.getTimeline().getTraitor() == 0) {
                changePic(false, traitor);
                mulitSettingContainer.setVisibility(View.VISIBLE);
                viewTraitorDesc.setVisibility(View.GONE);
            } else {
                changePic(true, traitor);
                mulitSettingContainer.setVisibility(View.GONE);
                viewTraitorDesc.setVisibility(View.VISIBLE);
            }
        }
    }

    private void setMultiTimelineStatus() {
        changePic(appSetting.getTimeline().getUser_tuijian() == 1, recommend);
        changePic(appSetting.getTimeline().getCoser() == 1, cos);
        changePic(appSetting.getTimeline().getDrawer() == 1, draw);
        changePic(appSetting.getTimeline().getWriter() == 1, novel);
        changePic(appSetting.getTimeline().getDaily() == 1, daily);
        changePic(appSetting.getTimeline().getGroup() == 1, group);
        changePic(appSetting.getTimeline().getZhipin() == 1, goods);
    }

    private void changePic(boolean on, ImageView iv) {
        if (on) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.switch_on, getContext().getTheme()));
            } else {
                iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.switch_on));
            }
        } else {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.switch_off, getContext().getTheme()));
            } else {
                iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.switch_off));
            }
        }
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.index_recommend_setting));
        mActionbarHelper.setActionCallbacks(new ActionbarHelper.OnActionCallbacks() {
            @Override
            public void onBackClick() {
                finishMainSetting();
            }

            @Override
            public void onActionClick(int id) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        finishMainSetting();
    }

    private void finishMainSetting() {
        if (null != appSetting && null != appSetting.getTimeline() && null != initialSetting) {
            if (!initialSetting.equals(appSetting.getTimeline()))
                BaseObserver.getInstance().notifyWatchers(BaseObserver.MAINSETTING_EVENT, "true", appSetting);
        }
        finish();
    }

    @Override
    protected void initUi() {
        mulitSettingContainer = findViewById(R.id.mulit_settings);
        viewTraitorDesc = findViewById(R.id.tv_traitor_desc);
        recommend = (ImageView) findViewById(R.id.recommend_iv);
        cos = (ImageView) findViewById(R.id.cos_iv);
        draw = (ImageView) findViewById(R.id.draw_iv);
        novel = (ImageView) findViewById(R.id.write_iv);
        daily = (ImageView) findViewById(R.id.daily_iv);
        group = (ImageView) findViewById(R.id.group_iv);
        goods = (ImageView) findViewById(R.id.goods_iv);
        traitor = (ImageView) findViewById(R.id.traitor_iv);
        ImageView[] imageViews = new ImageView[]{recommend, cos, draw, novel, daily, group, goods, traitor};
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i].setOnClickListener(this);
        }
    }

    @Override
    protected void initData() {
        MainSettingHelper.getMainSettings(this, new MainSettingHelper.MainSettingListener() {
            @Override
            public void getMainSettingSuccess(AppSetting appSetting) {
                MainSettingActivity.this.appSetting = appSetting;
                initialSetting = (TimeLineSetting) appSetting.getTimeline().clone();
                setStatus();
                mProgressbarHelper.onSuccess();
            }

            @Override
            public void setMainSettingSuccess(String response) {

            }

            @Override
            public void onFail() {
                mProgressbarHelper.onFailed();
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (appSetting == null || appSetting.getTimeline() == null) {
            return;
        }
        TimeLineSetting timeLineSetting = appSetting.getTimeline();
        String type;
        int is_open;
        switch (v.getId()) {
            case R.id.draw_iv:
                type = MainSettingHelper.TYPE_DRAWER;
                is_open = Math.abs(timeLineSetting.getDrawer() - 1);
                break;
            case R.id.write_iv:
                type = MainSettingHelper.TYPE_WRITER;
                is_open = Math.abs(timeLineSetting.getWriter() - 1);
                break;
            case R.id.cos_iv:
                type = MainSettingHelper.TYPE_COSER;
                is_open = Math.abs(timeLineSetting.getCoser() - 1);
                break;
            case R.id.daily_iv:
                type = MainSettingHelper.TYPE_DAILY;
                is_open = Math.abs(timeLineSetting.getDaily() - 1);
                break;
            case R.id.recommend_iv:
                type = MainSettingHelper.TYPE_USER_TUIJIAN;
                is_open = Math.abs(timeLineSetting.getUser_tuijian() - 1);
                break;
            case R.id.group_iv:
                type = MainSettingHelper.TYPE_GROUP;
                is_open = Math.abs(timeLineSetting.getGroup() - 1);
                break;
            case R.id.goods_iv:
                type = MainSettingHelper.TYPE_ZHIPIN;
                is_open = Math.abs(timeLineSetting.getZhipin() - 1);
                break;
            case R.id.traitor_iv:
                type = MainSettingHelper.TYPE_TRAITOR;
                is_open = Math.abs(timeLineSetting.getTraitor() - 1);
                break;
            default:
                return;
        }
        setMainSetting(type, is_open, v);
    }

    private void setMainSetting(final String type, final int is_open, final View v) {
        waitLightDialog = new WaitLightDialog(getContext(), R.style.BestDialog);
        waitLightDialog.show();
        MainSettingHelper.setMainSetting(this, type, is_open, new MainSettingHelper.MainSettingListener() {
            @Override
            public void getMainSettingSuccess(AppSetting appSetting) {
            }

            @Override
            public void setMainSettingSuccess(String response) {
                ImageView iv = (ImageView) v;
                setMainSettingView(type, is_open);
                changePic(is_open == 1, iv);
                waitLightDialog.dismiss();
            }

            @Override
            public void onFail() {
                waitLightDialog.dismiss();
            }
        });
    }

    private void setMainSettingView(String type, int is_open) {
        if (MainSettingHelper.TYPE_TRAITOR.equals(type)) {
            if (is_open == 1) {
                mulitSettingContainer.setVisibility(View.GONE);
                viewTraitorDesc.setVisibility(View.VISIBLE);
            } else {
                mulitSettingContainer.setVisibility(View.VISIBLE);
                viewTraitorDesc.setVisibility(View.GONE);
            }
            appSetting.getTimeline().setTraitor(is_open);
        } else if (MainSettingHelper.TYPE_DRAWER.equals(type)) {
            appSetting.getTimeline().setDrawer(is_open);
        } else if (MainSettingHelper.TYPE_WRITER.equals(type)) {
            appSetting.getTimeline().setWriter(is_open);
        } else if (MainSettingHelper.TYPE_COSER.equals(type)) {
            appSetting.getTimeline().setCoser(is_open);
        } else if (MainSettingHelper.TYPE_DAILY.equals(type)) {
            appSetting.getTimeline().setDaily(is_open);
        } else if (MainSettingHelper.TYPE_USER_TUIJIAN.equals(type)) {
            appSetting.getTimeline().setUser_tuijian(is_open);
        } else if (MainSettingHelper.TYPE_GROUP.equals(type)) {
            appSetting.getTimeline().setGroup(is_open);
        } else if (MainSettingHelper.TYPE_GROUP_POST.equals(type)) {
            appSetting.getTimeline().setGroup_post(is_open);
        } else if (MainSettingHelper.TYPE_ZHIPIN.equals(type)) {
            appSetting.getTimeline().setZhipin(is_open);
        }
    }
}
